/**
 * Created by joustany on 17/11/18.
 */
var page = 1
var size = 10
$(function () {
    getShowView(page,size)
})
function getShowView(page,size) {
    var params = JSON.parse(window.localStorage.user)
    var redirect = JSON.parse(decodeURIComponent(params['redirect']));
    $.ajax({
        url: configApi('/user-coupon/index'),
        data: {
            access_token:redirect.token,
            page:page,
            pageSize:size,
            usable:'1',
        },
        dataType: 'json',
        type: "POST",
    }).done(function (res) {
        console.log(res)
        if (res.success){
            packetView(res.data.list)
            pageCount = res.data.pages.pageCount
        }else {
            layer.open({
                content: res.message
                ,skin: 'msg'
                ,time: 2 //2秒后自动关闭
            });
        }
    }).fail(function () {
        layer.open({
            content: "服务器异常,请退出重试"
            ,skin: 'msg'
            ,time: 2 //2秒后自动关闭
        });
    })
}
function packetView(packet) {
    console.log(packet.length)
    var html = ''
    if(packet.length == 0){
        html +='<div class="icon"><img src="./images/ui_icon/空箱子.png" alt=""></div>'
        html +='<div class="empty">好像空空如也！</div>'
        html +='<div class="focus">关注蚂蚁鲜生部落群，搜刮红包</div>'
    }else {
        $.each(packet,function (index,item) {
            // console.log(item)
            html +='<div class="vouchers-content">'
            html +='<div class="voucher">'
            html +='<div class="voucher—number">￥<span class="num">' + item.money+'</span></div>'
            html +=' </div>'
            html +='<div class="voucher-date">'
            html +='<span class="title">'+ item.title+'</span>'
            html +='<span class="full">'+ item.use_rule +'</span>'
            html +='<span class="unlimited">不限品类</span>'
            html +=' <span class="indate">有效期至:&nbsp;<span class="date">'+ item.etime+'</span></span>'
            html +='</div>'
            html +='</div>'
        })
    }
    $('.vouchers .li').append(html)
    console.log($('.vouchers .li').height())
    console.log($('.vouchers .li').height())
    if ($('.vouchers').height()>$('.container').height()){
        $('.load').text('上拉加载更多')

    }else {
        $('.load').text('没有更多了')
    }
    loding = true
}
$(document).on('click','.getback',function () {
    window.location.href = './index.html'
})
var loding = true
$(".container").scroll(function() {
    var $this     = $(this),
        viewH     = $(this).height(),//可见高度
        contentH  = $(this).get(0).scrollHeight,//内容高度
        scrollTop = $(this).scrollTop();//滚动高度
//if(contentH - viewH - scrollTop <= 100) { //到达底部100px时,加载新内容
    if (!loding){
        return
    }
    if (scrollTop / (contentH - viewH) >= 0.9) { //到达底部100px时,加载新内容
        $('.load').text('加载中...')
        if (page == pageCount){ //当前页数等于总页数
            $('.load').text('- 到底了 -')
        }else {
            page++
            console.log(page)
            getShowView(page,size)
            loding = false

        }
    }

})
// pushHistory();
// window.addEventListener("popstate", function(e) {
//     window.location.href = './me.html'
//     params.removeItem('packet')
// }, false);
// function pushHistory() {
//     var state = {
//         title: "title",
//         url: "#"
//     };
//     window.history.pushState(state, "title", "");
// }
